<?php

namespace App\Models\Article;

use App\Models\Common\CommonModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Builder;

class ArticleInfo extends CommonModel
{
    use HasFactory;

    protected $table = 'article_info';

    public function getClassify() {
        return $this->hasOne(ArticleClassify::class,'id','classify_id')->withDefault(['name'=>'未知']);
    }

    public function getRecords(array $conditions): Builder {
        $records = parent::getRecords($conditions); // TODO: Change the autogenerated stub
        if(!empty($conditions['classifyId']) && $conditions['classifyId']!='all'){
            $records = $records->where('classify_id',$conditions['classifyId']);
        }
        if(!empty($conditions['upDown'])){
            $records = $records->where('up_down',$conditions['upDown']);
        }
        if(!empty($conditions['title']) ){
            $records = $records->where('title','like','%'.$conditions['title'].'%');
        }
        if(!empty($conditions['keyWord']) ){
            $records = $records->where('key_word','like','%'.$conditions['keyWord'].'%');
        }
        if(!empty($conditions['description']) ){
            $records = $records->where('description','like','%'.$conditions['description'].'%');
        }
        if(!empty($conditions['content']) ){
            $records = $records->where('content','like','%'.$conditions['content'].'%');
        }
        if(!empty($conditions['year']) ){
            $records = $records->where('year',$conditions['year']);
        }
        if(!empty($conditions['month']) ){
            $records = $records->where('month',$conditions['month']);
        }
        return $records->orderBy('is_top','asc')->orderBy('id','desc');
    }


}
